package com.example.book.sys.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.book.base.BaseController;
import com.example.book.base.Result;
import com.example.book.common.utils.Constants;
import com.example.book.domain.sys.SysDept;
import com.example.book.sys.dept.service.SysDeptService;
import com.example.book.domain.sys.SysPost;
import com.example.book.sys.post.service.SysPostService;
import com.example.book.domain.sys.SysDeptPost;
import com.example.book.domain.sys.SysUser;
import com.example.book.domain.sys.SysUserRole;
import com.example.book.sys.user.service.SysDeptPostService;
import com.example.book.sys.user.service.SysUserRoleService;
import com.example.book.sys.user.service.SysUserService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author haocj
 * 用户控制器
 */
@RestController
@RequestMapping(value = "/sys/sysUser")
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService userService;
    @Autowired
    private SysUserRoleService userRoleService;
    @Autowired
    private SysDeptPostService sysDeptPostService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysPostService sysPostService;

    @ModelAttribute
    public SysUser get(@RequestParam(required=false) String id) {
        if (StringUtils.isNotBlank(id)){
            return userService.get(id);
        }else{
            return new SysUser();
        }
    }

    /**
     * 分页查询
     * @param sysUser
     * @return
     */
    @RequestMapping(value = "/info")
    public PageInfo<SysUser> findList(SysUser sysUser) {
        return userService.findPage(sysUser);
    }

    @RequestMapping(value = "/save")
    public Result save(@RequestParam("rids") String[] ids, SysUser sysUser) {

        if (!StringUtils.isNotBlank(sysUser.getId())) {
            SysUser sys = new SysUser();
            sys.setLoginName(sysUser.getLoginName());
            SysUser user = userService.findByNameOrPass(sys);

            if (user != null) {
                return Result.error("登录名已存在，请更换！");
            }
            // 账户状态
            sysUser.setLoginFlag(Constants.LOGIN_FLAG_0);
        }

        userService.save(sysUser, ids);
        return Result.success("保存成功！");
    }

    /**
     * 更新数据
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/update")
    public Result<JSONObject> update(String id) {

        SysUser sysUser = userService.get(id);

        SysUserRole userRole = new SysUserRole();
        userRole.setUserId(sysUser.getId());

        List<SysUserRole> list = userRoleService.findList(userRole);

        List<String> ids = new ArrayList<String>();
        for (SysUserRole role : list) {
            ids.add(role.getRoleId());
        }

        SysDeptPost sysDeptPost = sysDeptPostService.get(sysUser.getId());

        JSONObject object = new JSONObject();
        object.put("data", sysUser);
        object.put("ids", ids);
        if (sysDeptPost != null) {
            SysPost post = new SysPost();
            post.setId(sysDeptPost.getPostId());
            SysPost sysPost = sysPostService.get(post);
            object.put("sysPost", sysPost);
            SysDept dept = new SysDept();
            dept.setId(sysDeptPost.getDeptId());
            SysDept sysDept = sysDeptService.get(dept);
            object.put("sysDept", sysDept);
        }
        return Result.success(object);
    }

    @RequestMapping(value = "/delete")
    public Result delete(@RequestParam("ids") String[] ids) {
        userService.delete(ids);
        return Result.success();
    }

    @RequestMapping(value = "/available")
    public Result available(String id) {
        SysUser sysUser = userService.get(id);

        if (sysUser.getId().equals(Constants.SUPER_ADMIN)){
            return Result.error("不能禁用超级管理员！");
        }

        String msg = null;
        if (sysUser.getLoginFlag().equals(Constants.LOGIN_FLAG_0)) {
            sysUser.setLoginFlag(Constants.LOGIN_FLAG);
            msg = "账户已被禁用！";
        } else {
            sysUser.setLoginFlag(Constants.LOGIN_FLAG_0);
            msg = "账户已成功启用！";
        }
        userService.save(sysUser);
        return Result.success(msg);
    }

}
